package com.system.blogAdmin.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
 * 作者： cnc
 * 创建时间：2017-09-26.
 * 版本：1.0
 */
public class ConfigUtils {
    private static Logger log = LoggerFactory.getLogger(ConfigUtils.class);



    private static java.util.Properties properties = new java.util.Properties();

    private static File file;
    /**
     * 静态块:加载.properties文件
     */
    static {
        try {
            properties.load(ConfigUtils.class.getClassLoader().getResourceAsStream("configuration.properties"));
            //		properties.load(new InputStreamReader(DadaConfigUtils.class.getClassLoader().getResourceAsStream("conf.properties"), "UTF-8"));
        } catch (Exception e) {
            log.error("配置文件加载失败：{}", e);
        }
    }

    public static String getProperties(String key) {
        String str = properties.getProperty(key);
        if (str == null || "".equals(str)) {
            str = "";
        }
        return str;
    }

    public static int getIntegerProperties(String key) {
        String str = properties.getProperty(key);
        if (str == null || "".equals(str)) {
            str = "";
        }
        return Integer.parseInt(str);
    }

    public static void save(String key, String value) {
        OutputStream fos = null;
        try {

            fos = new FileOutputStream(file);
            properties.setProperty(key, value);
            properties.store(fos, "Update '" + key + "' value");
            fos.close();
        } catch (IOException e) {
            log.error("配置文件读写失败：{}", e.getMessage());
        } finally {
            try {
                fos.close();
            } catch (IOException e) {
                log.error("配置文件读写失败：{}", e.getMessage());
            }
        }
    }

    public static void main(String[] args) {
        String s = properties.getProperty("YUNNIAO_WAREHOUSE");
        System.out.println(s);
    }

}
